//
//  SoloMusicView.m
//  K-Music
//
//  Created by 小晓 on 16/2/17.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "SoloMusicView.h"
#import "KMusicMsgDataModel.h"
#import "MusicChorusTableViewCell.h"
#import "MusicAllAndAloneViewController.h"
#import "YYplayerVC.h"

@implementation SoloMusicView

-(id)initWithFrame:(CGRect)frame superViewCtr:(UIViewController *)superCtr{
    self = [super initWithFrame:frame];
    if(self){
        _superViewCtr = superCtr;
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:(UITableViewStylePlain)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.allowsSelection = YES;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self addSubview:_tableView];
        
        [self requestData];
        
    }
    return self;
}

-(void)requestData
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:@"/api/record_rank"];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        _dataArray = [NSMutableArray array];
        for(NSDictionary * contentDict in contentArray){
            KMusicMsgDataModel * msgDataModel = [[KMusicMsgDataModel alloc]init];
            
            msgDataModel.avatar = [contentDict objectForKey:@"pictureUrl"];
            msgDataModel.musicDes = [contentDict objectForKey:@"description"];
            msgDataModel.name = [contentDict objectForKey:@"name"];
            msgDataModel.nickName =[NSString stringWithFormat:@"%d",(int)[contentDict objectForKey:@"User_id"]];
            msgDataModel.isSolo = YES;
            [msgDataModel fixedString];
            [_dataArray addObject:msgDataModel];
        }
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicChorusTableViewCell"];
    if ( !cell ) {
        cell = [[MusicChorusTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MusicChorusTableViewCell"];
    }
    KMusicMsgDataModel *musicModel = [_dataArray objectAtIndex:indexPath.row];
    [(MusicChorusTableViewCell *)cell updataCellViewWithHeadData:musicModel
     superViewCtr:_superViewCtr];
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat contentWidth = tableView.frame.size.width;
    // 用何種字體進行顯示
    UIFont *font = [UIFont systemFontOfSize:13];
    // 該行要顯示的內容
    NSString *content = ((KMusicMsgDataModel *)[_dataArray objectAtIndex:indexPath.row]).musicDes;
    // 計算出顯示完內容需要的最小尺寸
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeCharacterWrap];
    
    return 100+size.height;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [((MusicAllAndAloneViewController*)_superViewCtr).textField resignFirstResponder];
    YYplayerVC* yy = [[YYplayerVC alloc]init];
    
    yy.hidesBottomBarWhenPushed = YES;
    
    yy.musicData= [User_information conversion].musicData[0];
    
    [yy setTracks:[Track remoteTracks]];
    
    [_superViewCtr.navigationController pushViewController:yy animated:YES];
}


@end
